package com.ilop.sthome.ui.activity.mine;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;

import com.aliyun.alink.linksdk.tmp.api.DeviceManager;
import com.aliyun.iot.aep.sdk.login.ILogoutCallback;
import com.aliyun.iot.aep.sdk.login.LoginBusiness;
import com.aliyun.iot.push.PushManager;
import com.example.common.base.BaseBindActivity;
import com.example.common.base.OnCallBackToRefresh;
import com.example.common.utils.SpUtil;
import com.ilop.sthome.ui.activity.main.StartActivity;
import com.ilop.sthome.ui.dialog.BaseDialog;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityAgreementBinding;

/**
 * @author skygge
 * @date 2019-10-25.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：用户协议
 */
public class AgreementActivity extends BaseBindActivity<ActivityAgreementBinding> implements ILogoutCallback {

    private boolean isAgree;
    private boolean isRegister;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_agreement;
    }

    @Override
    protected void initialize() {
        super.initialize();
        isAgree = SpUtil.getBoolean(mContext, "isAgree", false);
        isRegister = getIntent().getBooleanExtra("isRegister", false);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void initView() {
        super.initView();
        mDBind.layoutAgreement.setVisibility(isRegister ? View.GONE : View.VISIBLE);
        mDBind.cbAgree.setChecked(isAgree);
        WebSettings webSettings = mDBind.userWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        mDBind.userWebView.setInitialScale(25);
        mDBind.userWebView.setWebViewClient(new WebViewClient());
    }

    @Override
    protected void initData() {
        super.initData();
        String userProtocol = ConfigureUtil.getUserProtocol(mContext);
        mDBind.userWebView.loadUrl(userProtocol);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivDefaultBack.setOnClickListener(view -> finish());
        mDBind.cbAgree.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b){
                BaseDialog mDialog = new BaseDialog(mContext, new OnCallBackToRefresh() {
                    @Override
                    public void onConfirm() {
                        SpUtil.putBoolean(mContext, "isAgree", false);
                        loginOut();
                    }

                    @Override
                    public void onCancel() {
                        mDBind.cbAgree.setChecked(true);
                    }
                });
                mDialog.setTitleAndButton(getString(R.string.exit_agreement), getString(R.string.dialog_btn_cancel), getString(R.string.ok));
                mDialog.show();
            }
        });
    }

    private void loginOut(){
        PushManager.getInstance().unbindUser();
        DeviceManager.getInstance().clearAccessTokenCache();
        LoginBusiness.logout(this);
    }

    @Override
    public void onLogoutSuccess() {
        DeviceDaoUtil.getInstance().deleteAllGateway();
        Intent intent = new Intent(mContext, StartActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @Override
    public void onLogoutFailed(int i, String s) {
        showToast(mContext.getString(R.string.failed));
    }
}
